//
//  ViewController.swift
//  JSTech
//
//  Created by 高得华 on 2019/9/29.
//  Copyright © 2020 华&梅. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    ///是否可以继续点击 重新请求数据 true不允许
    var is_cxqq = false

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.post_data()
        MDHSwiftHeader.addTap(view: self.view) { [weak self] (tap) in
            if let weakSelf = self {
                if !weakSelf.is_cxqq {
                    ShowHUDMessageView(weakSelf.view)
                    weakSelf.post_data()
                }
            }
        }
    }
    
    /// 网络请求 = 请求证书及公钥
    fileprivate func post_data() -> () {
        if self.is_cxqq {
            return
        }
        self.is_cxqq = true
        
        let manager = AFHTTPSessionManager()
        manager.requestSerializer.timeoutInterval = 30;
        /// 支持解析
        manager.responseSerializer.acceptableContentTypes = Set(arrayLiteral: "application/json", "text/json", "text/javascript", "text/html", "text/plain", "text/html; charset=UTF-8", "image/*", "text/xml")
        manager.requestSerializer = AFHTTPRequestSerializer.init()
        
        let params = [String:String]()
        let headers = [String:String]()
        
        var url = ""
        #if DEBUG
        url = "http://139.129.223.116"
        #else
        url = "http://qminter.kiss8.net/"
        #endif

        manager.post(url, parameters: params, headers: headers, progress: nil, success: { [weak self] (task, responseObject) in
            #if DEBUG
            print("======== https 的 配置信息: \(String(describing: responseObject))")
            #endif
            if responseObject is NSDictionary {
                let result = MMConfigReModel.mj_object(withKeyValues: responseObject)
                if let model = result {
                    if model.status == MHHTTPResponseCode.success.rawValue {///成功
                        let rr = model.result
                        MDHSwiftHeader.save_cache(datas: rr, key: url)
                        AppDelegate.shared()?.dataHttps = rr
                        DismissHud(self?.view)
                    }else{
                        ShowHUDMesHidden(model.text, self?.view)
                        self?.is_cxqq = false
                    }
                }
            }else{
                self?.is_cxqq = false
            }
            task.cancel()
        }) { [weak self] (task, error) in
            #if DEBUG
            let err = error as NSError
            print("======== https 的 配置信息: \(err)")
            AlertViewShowOne("配置信息错误\(err)")
            #endif
            ShowHUDMesHidden(error.localizedDescription, self?.view)
            task?.cancel()
            self?.is_cxqq = false
        }
    }
}


//MARK: - ==== 配置Model
class MMConfigReModel: MDHPTModel {
    ///status
    @objc var status = 0
    ///text
    @objc var text = ""
    ///result
    @objc var result = MMConfigModel()
}

///result
class MMConfigModel: MDHPTModel {
    ///api_url
    @objc var api_url = ""
    ///api_url_ssl
    @objc var api_url_ssl = ""
    //api_url_ssl_ios
    @objc var api_url_ssl_ios = ""
    ///api_url_ssl_md5
    @objc var api_url_ssl_md5 = ""
    ///stealth_policy_url
    @objc var stealth_policy_url = ""
    ///user_agreement_url
    @objc var user_agreement_url = ""
    
    ///h5域名
    @objc var mobile_url = ""
}
